-- SharedMedia access
local LibSharedMedia 	= LibStub("LibSharedMedia-3.0")
Avion2_ComicMedia 		= {
	loader 				= CreateFrame("Frame", "Avion2_ComicMedia", UIParent)
}
-- Note: This will be garbaged!


-- Injection function
function Avion2_ComicMedia:Inject()
	-- Self who's that?
	local self = Avion2_ComicMedia
	
	-- Not loaded?
	if (not self.textureList or not self.soundList) then 
		return 
	end

	-- Register each texture to Avion2
	for name, file in pairs(self.textureList) do
		Avion2Core:AddTexture(name, "Interface\\AddOns\\Avion2_ComicMedia\\Textures\\" .. file)
	end
	self.textureList = nil

	-- Register each sound to SharedMedia
	for name, file in pairs(self.soundList) do
		LibSharedMedia:Register(LibSharedMedia.MediaType.SOUND, name, "Interface\\AddOns\\Avion2_ComicMedia\\Sounds\\" .. file)
	end
	self.soundList = nil
	
	-- Register functions to Avion2
	Avion2Core:AddFunction("Square", 	"return param*param")
	Avion2Core:AddFunction("Root", 		"return math.sqrt(param)")
	Avion2Core:AddFunction("Exp", 		"return math.pow(param, 10) / 1024.0")
	Avion2Core:AddFunction("Log", 		"return math.log(1 + param * 100) / 4.6052")
	Avion2Core:AddFunction("Sin(x)/x", 	[[
		if (param < 0.95) then
			return (1 - (math.sin(param * 100) / (param * 100) + 0.2172) / 1.2172) * (0.8 * (1-param) + 1.2 * param)
		else
			return param
		end
	]])
	Avion2Core:AddFunction("Wave1", 	[[
		return math.sin(param * 62.83) / 5 + param
	]])
	Avion2Core:AddFunction("Wave2", 	[[
		return math.sin((param * 39.9988 + 11.4250)) / 5 + (param * 0.6366 + 0.1818);
	]])
	Avion2Core:AddFunction("Jitter", 	[[
		if (param < 0.1429) then
			return 2 * param;
		elseif (param < 0.2858) then
			return -param + 0.4287;
		elseif (param < 0.4287) then
			return 2*param - 0.4287;
		elseif (param < 0.5716) then
			return -param + 0.8574;
		elseif (param < 0.7145) then
			return 2*param - 0.8574;
		elseif (param < 0.8574) then
			return -param + 1.2861;
		else
			return 2*param - 1.2861;
		end
	]])
	
	-- Disable self
	self.loader:SetScript("OnLoad", nil)
	self.loader:SetScript("OnUpdate", nil)
	self.loader:UnregisterEvent("OnUpdate")
	self.loader:UnregisterEvent("OnLoad")
	self.loader:SetParent(nil)
	self.loader:Hide()
	self.loader 		= nil
	self.Inject			= nil
	Avion2_ComicMedia	= nil
	LibSharedMedia 		= nil
end